package peer.p2pnet.message;

import java.net.DatagramPacket;

import peer.Config;
import peer.Message;
import peer.tools.ByteIntSwitch;
import peer.tools.ByteShortSwitch;
import peer.tools.Mdb;

public class DBSynMessage1 extends Message{
	public short sType=peer.tools.MessageType.sType_DBSynMessage1;
	Mdb mdb;
	int eventOffset2;
	byte[] dpData=new byte[28];
	int sessionId;
	public DBSynMessage1(Mdb mdb,int eventOffset){
		this.mdb=mdb;
		this.eventOffset2=eventOffset;
		ByteShortSwitch.toByteArray(ver, dpData, 0);  //add: ver
		ByteShortSwitch.toByteArray(sType, dpData, 2);  //add: service type
		ByteIntSwitch.toByteArray(Config.sessionId, dpData, 4);  //add: sessionId
		System.arraycopy(mdb.md, 0, dpData, 8,16);
		ByteIntSwitch.toByteArray(eventOffset, dpData, 24);
		dp=new DatagramPacket(dpData,dpData.length);
	}
	
	public DBSynMessage1(DatagramPacket dp){
		this.dp=dp;
		dpData=dp.getData();
		ver=ByteShortSwitch.toShort(dpData, 0);
		sType=ByteShortSwitch.toShort(dpData, 2);
		sessionId=ByteIntSwitch.toInt(dpData, 4);
		byte[] md=new byte[16];
		System.arraycopy(dpData, 8, md, 0,16);
		mdb=new Mdb(md);
		eventOffset2=ByteIntSwitch.toInt(dpData, 24);
	}
	
	public int getSessionId(){
		return sessionId;
	}
	public Mdb getMdb(){
		return mdb;
	}
	
	public int getEventOffset2(){
		return eventOffset2;
	}
}
